package com.cf.core.player.impl;

import com.cf.common.util.encrypt.MD5coding;
import com.cf.common.util.page.Paging;
import com.cf.core.player.UserInfo;
import com.cf.core.player.UserInfoService;
import com.cf.game.service.constants.SysConfigModel;
import com.cf.game.service.model.UserInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * game-server-DefaultUserInfoService
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/5/10 下午 05:16
 */
public class DefaultUserInfoService implements UserInfoService {

    private Random random = new Random();

    private List<UserInfoEntity> userInfoEntityList = new ArrayList();


    @Autowired
    private com.cf.game.service.service.UserInfoService userInfoService;


    @PostConstruct
    public void init() {
        com.cf.game.service.pojo.UserInfo userInfo = new com.cf.game.service.pojo.UserInfo();
        userInfo.setStatusList(Arrays.asList(3));
        Paging<UserInfoEntity> virtualUserByPage = userInfoService.getVirtualUserByPage(userInfo, 1, Integer.MAX_VALUE);
        userInfoEntityList.addAll(virtualUserByPage.getList());
    }


    @Override
    public UserInfo getUserInfo(String userId) {
        Integer uId = Integer.parseInt(userId);
        com.cf.game.service.pojo.UserInfo info = userInfoService.selectUserNameAndUserImg(uId);
        UserInfo userInfo = new DefaultUserInfo(info.getId(), null, info.getDefaultName(), info.getDefaultHeadImg());
        return userInfo;
    }

    @Override
    public UserInfo virtualUserInfo() {
        UserInfoEntity userInfoEntity = userInfoEntityList.get(random.nextInt(userInfoEntityList.size()));
        DefaultUserInfo defaultUserInfo = new DefaultUserInfo(
                (int) (userInfoEntity.getId() - SysConfigModel.USER_NO_INCRE),
                MD5coding.getInstance().encrypt((userInfoEntity.getId() - SysConfigModel.USER_NO_INCRE) + ""),
                userInfoEntity.getDefaultName(),
                userInfoEntity.getDefaultHeadImg());
        return defaultUserInfo;
    }

}
